﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;

namespace dbgptc
{
    public partial class TestForm : Form
    {
        DbgpTunnel _tunnel;
        string _settingPath;
        bool _canClose = false;

        public TestForm()
        {
            InitializeComponent();

            _settingPath = System.IO.Path.Combine(Application.UserAppDataPath, "settings.xml");
            loadTunnelSettings();
        }

        private bool startTunneling()
        {
            try {
                _tunnel = new DbgpTunnel(tbTunnelAddr.Text, int.Parse(tbTunnelPort.Text), tbIdeKey.Text);
                _tunnel.run(tbDbgpAddr.Text, int.Parse(tbDbgpPort.Text));
                return true;
            } catch (Exception ex) {
                stopTunneling();
                MessageBox.Show(ex.Message);
            }
            return false;
        }

        private void stopTunneling()
        {
            if (_tunnel != null) {
                _tunnel.stop();
                _tunnel = null;
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Minimized;
            ShowInTaskbar = false;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (_tunnel != null) {
                stopTunneling();
                btnOk.Text = "&start";
            } else {
                if (startTunneling()) {
                    btnOk.Text = "&stop";
                    saveTunnelSettings();
                }
            }
        }

        private void loadTunnelSettings()
        {
            TunnelSettings settings = TunnelSettings.load(_settingPath);

            tbTunnelAddr.Text = settings.tunnelAddress;
            tbTunnelPort.Text = settings.tunnelPort.ToString();
            tbDbgpAddr.Text = settings.dbgpSessionAddress;
            tbDbgpPort.Text = settings.dbgpSessionPort.ToString();
            tbIdeKey.Text = settings.ideKey;
        }

        private void saveTunnelSettings()
        {
            TunnelSettings settings = new TunnelSettings();
            settings.tunnelAddress = tbTunnelAddr.Text;
            settings.tunnelPort = int.Parse(tbTunnelPort.Text);
            settings.dbgpSessionAddress = tbDbgpAddr.Text;
            settings.dbgpSessionPort = int.Parse(tbDbgpPort.Text);
            settings.ideKey = tbIdeKey.Text;
            settings.save(_settingPath);
        }

        private void menuSettings_Click(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Normal;
            ShowInTaskbar = true;
        }

        private void menuClose_Click(object sender, EventArgs e)
        {
            _canClose = true;
            Close();
        }

        private void TestForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing && !_canClose) {
                e.Cancel = true;
                WindowState = FormWindowState.Minimized;
            }
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            WindowState = FormWindowState.Normal;
            ShowInTaskbar = true;
        }

        private void TestForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            stopTunneling();
        }
    }
}
